package org.example;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        List<String> originalList = new ArrayList<>();
        originalList.add("This is a test [str.pngtring2] example.");
        originalList.add("Another example with [another1|another2].");
        originalList.add("No match here.");
        originalList.add("Yet another [exampl/e1| exam   ple2] case.");
        originalList.add("Image example [image.png]");
        originalList.add("Example without pipe [exampleWithoutPipe.png]");

        List<String> extractedStrings = new ArrayList<>();// 创建一个新列表，用于存储提取的字符串

        Pattern pattern = Pattern.compile("\\[(.*?)\\|(.*?)\\]");
        for (int i = 0; i < originalList.size(); i++) {
            String str = originalList.get(i);
            Matcher matcher = pattern.matcher(str);
            if (matcher.find()) {
                String firstGroup = matcher.group(1);
                String secondGroup = matcher.group(2);
                if (firstGroup.contains("/")) {
                    String modifiedStr = str.replaceFirst("\\[(.*?)\\|(.*?)\\]", "[$2]");
                    originalList.set(i, modifiedStr);
                }
                extractedStrings.add(secondGroup);
            } else {
                // 检查是否包含 [任意字符串] 且不包含 "|" 和 ".png"
                Pattern singlePattern = Pattern.compile("\\[(.*?)\\]");
                Matcher singleMatcher = singlePattern.matcher(str);
                if (singleMatcher.find()) {
                    String firstGroup = singleMatcher.group(1);
                    if (!firstGroup.contains("|") && !firstGroup.contains(".png")) {
                        extractedStrings.add(firstGroup);
                    }
                }
            }
        }

        System.out.println("Modified List: " + originalList);
        System.out.println("Extracted Strings: " + extractedStrings);
    }
}
